<template>
  <div class="department-container">
    <el-card>
      <div slot="header">
        <span>部门管理</span>
        <el-button style="float: right" type="primary" size="small" @click="handleAdd">
          添加部门
        </el-button>
      </div>
      
      <el-table :data="departmentList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="name" label="部门名称" width="120"></el-table-column>
        <el-table-column prop="manager" label="部门主管"></el-table-column>
        <el-table-column prop="employeeCount" label="员工数量"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="部门主管" prop="manager">
          <el-input v-model="form.manager"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Department',
  data() {
    return {
      departmentList: [
        { id: 1, name: '技术部', manager: '张三', employeeCount: 20, description: '负责公司技术研发' },
        { id: 2, name: '人事部', manager: '李四', employeeCount: 5, description: '负责人力资源管理' },
        { id: 3, name: '财务部', manager: '王五', employeeCount: 8, description: '负责公司财务管理' },
        { id: 4, name: '市场部', manager: '赵六', employeeCount: 12, description: '负责市场营销' }
      ],
      dialogVisible: false,
      dialogTitle: '添加部门',
      form: {
        name: '',
        manager: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        manager: [{ required: true, message: '请输入部门主管', trigger: 'blur' }],
        description: [{ required: true, message: '请输入部门描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleAdd() {
      this.dialogTitle = '添加部门'
      this.form = {
        name: '',
        manager: '',
        description: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑部门'
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该部门?', '提示', {
        type: 'warning'
      }).then(() => {
        // 这里应该调用删除API
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 这里应该调用添加/编辑API
          this.$message.success(this.dialogTitle + '成功')
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>

<style scoped>
.department-container {
  padding: 20px;
}
</style> 